package com.jiantong.web.open.app.lawEnforcing;

import com.jiantong.lawEnforcing.model.WritRelevant;
import com.jiantong.lawEnforcing.model.WritType;
import com.jiantong.lawEnforcing.service.WritTypeService;
import com.jiantong.web.core.enums.ExceptionCode;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Map;

/**
 * 文书类型API
 * Created by mz03 on 2017/9/7.
 */
@Slf4j
@OpenBean
public class WritTypeApis {
    @RpcConsumer
    private WritTypeService writTypeService;


    /**
     * 根据id查询一条信息
     * @param id
     * @return
     */
    @OpenMethod(
            key = "find.writType.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public WritType findById(Long id){
        Response<WritType> response = writTypeService.finById(id);
        if (!response.isSuccess()){
            log.error("failed to find writType read by id = {}, cause : {}", id, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }


    /**
     * 创建一条信息
     * @param writType
     * @return
     */
    @OpenMethod(
            key = "create.writType",
            paramNames = {"writType"},
            httpMethods = RequestMethod.POST
    )
    public Long create(WritType writType){
        Response<Long> response = writTypeService.create(writType);
        if(!response.isSuccess()){
            log.error("failed to create writType read by writType = {}, cause : {}", writType, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    /**
     * 更新单据
     */
    @OpenMethod(
            key = "update.writType",
            paramNames = {"writType"},
            httpMethods = RequestMethod.PUT
    )
    public Boolean update(WritType writType){
        if(writType.getId() == null){
            log.error("id should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should be not null");
        }
        Response<Boolean> response = writTypeService.update(writType);
        if(!response.isSuccess()){
            log.error("failed to update writType");
            throw new OPServerException(ExceptionCode.DATA_UPDATE_FAIL.value(),
                    "failed to update writType");
        }
        return response.getResult();
    }

    /**
     * 根据type或者detailed_type分页查询
     * @param pageNo
     * @param pageSize
     * @param type
     * @param detailed_type
     * @return
     */
    @OpenMethod(
            key = "find.paging.writType.by.type",
            paramNames = {"pageNo","pageSize","type","detailed_type"},
            httpMethods = RequestMethod.GET
    )
    public Paging<WritType> paging(
            Integer pageNo,
            Integer pageSize,
            Integer type,
            Integer detailed_type
    ){
        Response<Paging<WritType>> pagingResponse = writTypeService.pagingByType(pageNo,pageSize,type,detailed_type);
        if(!pagingResponse.isSuccess()){
            log.error("failed to find paging writType type {}, cause : {}", type, pagingResponse.getError());
            throw new OPServerException(pagingResponse.getError());
        }
        return pagingResponse.getResult();
    }

    //查询说有的文书一级类型Map<type,name>
    //参数: null
    //返回值: Map<type,name>
    @OpenMethod(
        key = "find.writType",
        httpMethods = RequestMethod.GET
    )
    public Map<Integer,String> allType(){
        Response<Map<Integer,String>> response = writTypeService.allType();
        if (!response.isSuccess()){
            log.error("failed to find allType, cause : {}",response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }



    //查询某一级类型下的所有二级类型Map<type,name>
    //参数: type
    //返回值: Map<type,name>
    @OpenMethod(
            key = "find.detailed.type",
            paramNames = {"type"},
            httpMethods = RequestMethod.GET
    )
    public Map<Integer,String> allDetailedTypeByType(Integer type){
        Response<Map<Integer,String>> response = writTypeService.allDetailedTypeByType(type);
        if (!response.isSuccess()){
            log.error("failed to find allDetailedType, cause : {}",response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }


}
